package com.bawei.module_my.view.view

import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PointF
import android.graphics.Rect
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View


class MyQuXianView : View {
    private var sumHeight = 0f //总控件的高度
    private var sumWidth = 0f //总空间的宽度
    private var maxTime = 0f //最大的时间 用来划分单位的 最小就是20 X1.2是为了给上方和下方预留空间
    private var linePaint: Paint? = null //线的画笔
    private var shadowPaint: Paint? = null //阴影的画笔
    private var mPaint: Paint? = null //曲线画笔
    private var circlePaint: Paint? = null //圆点画笔
    private var circlePaint2: Paint? = null //圆点画笔
    private var scorePaint: Paint? = null
    private var textPaint: Paint? = null //文字的画笔
    private var timeList: ArrayList<Int>? = null //金币
    private var dataList: ArrayList<String>? = null //底部的时间
    private var oneHeight = 0f //每一个小段所要分成的高
    private var oneWidth = 0f //每一个小段所要分成的宽
    private var buttomHeiht = 0f //给底部一排日期预留出的时间
    private var baseLinePath: Path? = null //折线路径
    private val smoothness = 0.36f //折线的弯曲率
    private var baseShadow: Paint? = null //折线下的阴影的画笔
    private var xyList: ArrayList<PointF>? = null //储存定好的坐标点的集合
    private var xyList2: ArrayList<PointF>? = null //储存定好的坐标点的集合

    constructor(context: Context?) : super(context) {
        initPaint()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initPaint()
    }

    /**
     * 初始化画笔
     *
     * @linpaint 线条画笔
     * @shadowPaint 阴影画笔
     */
    private fun initPaint() {
        //画线的画笔1 收入
        linePaint = Paint()
        linePaint!!.color = Color.parseColor("#17CAAA")
        linePaint!!.isAntiAlias = true
        linePaint!!.textSize = dp2px(context, 12f).toFloat()
        linePaint!!.strokeWidth = dp2px(context, 1f).toFloat()


        //画背景的画笔
        shadowPaint = Paint()
        shadowPaint!!.color = Color.parseColor("#CBF2ED")
        shadowPaint!!.isAntiAlias = true
        //画最下方文字的画笔
        textPaint = Paint()
        textPaint!!.color = Color.parseColor("#999999")
        textPaint!!.isAntiAlias = true
        textPaint!!.textSize = dp2px(context, 11f).toFloat()
        circlePaint = Paint(Paint.ANTI_ALIAS_FLAG)
        circlePaint!!.color = Color.parseColor("#8BE4D4")
        circlePaint!!.strokeWidth = dp2px(context, 2f).toFloat()
        circlePaint!!.style = Paint.Style.STROKE
        circlePaint2 = Paint(Paint.ANTI_ALIAS_FLAG)
        circlePaint2!!.color = Color.WHITE
        circlePaint2!!.style = Paint.Style.FILL
        baseShadow = Paint()
        baseShadow!!.isAntiAlias = true
        baseShadow!!.color = Color.WHITE and 0x40FFFFFF or 0x10000000
        baseShadow!!.style = Paint.Style.FILL
        buttomHeiht = dp2px(35f).toFloat() //线距离底部高度
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint!!.color = Color.parseColor("#17CAAA")
        mPaint!!.strokeWidth = dp2px(context, 2f).toFloat()
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.strokeCap = Paint.Cap.ROUND
        scorePaint = Paint(Paint.ANTI_ALIAS_FLAG)
        scorePaint!!.style = Paint.Style.STROKE
        scorePaint!!.strokeCap = Paint.Cap.ROUND
        scorePaint!!.color = Color.parseColor("#DDDDDD")
        scorePaint!!.strokeWidth = dp2px(0.5f).toFloat()
        baseLinePath = Path()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        sumHeight = measuredHeight.toFloat()
        sumWidth = measuredWidth.toFloat()
    }

    private fun measure() {
        maxTime = 100f //最大分数为100
        for (i in 0..6) {
            if (maxTime <= timeList!![i]) {
                maxTime = timeList!![i].toFloat()
            }
        }

        if (maxTime < 20) {
            maxTime = 20f
        }
        val text = "v币"
        val rect = Rect()
        textPaint!!.getTextBounds(text, 0, text.length, rect)
        oneHeight = (sumHeight - buttomHeiht - 2 * rect.height()) / maxTime
        oneWidth = sumWidth / 28
    }

    /**
     * 更新金币使用
     */
    fun updateTime(timeList: ArrayList<Int>?, bottomList: ArrayList<String>?) {
        this.timeList = timeList
        dataList = bottomList
        if (this.timeList != null && this.timeList!!.size > 0  &&dataList != null && dataList!!.size > 0) {
            invalidate()
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (timeList == null || dataList == null) return
        maxTime = getMaxTime(timeList)
        measure()
        toGetXy() //获取x和y的坐标
        toDrawLine(canvas)
    }

    private fun toGetXy() {
        val leftWidth = dp2px(13f) //距离右边宽度
        xyList = ArrayList()

        for (i in 0..6) {
            val x = oneWidth + i * 4 * oneWidth
            val time = timeList!![i].toFloat() //每点时间
            val y = sumHeight - oneHeight * time
            xyList!!.add(PointF(x + leftWidth, y - buttomHeiht))
        }

    }

    /**
     * 画线
     */
    private fun toDrawLine(canvas: Canvas) {
        if (xyList == null || xyList!!.size == 0) {
            return
        }

        val NewPoints: MutableList<PointF> = ArrayList()
        NewPoints.addAll(xyList!!)
        var lX = 0f
        var lY = 0f
        baseLinePath!!.reset()
        baseLinePath!!.moveTo(NewPoints[0].x, NewPoints[0].y)
        for (i in 1 until NewPoints.size) {
            val p = NewPoints[i]
            val firstPointF = NewPoints[i - 1]
            val x1 = firstPointF.x + lX
            val y1 = firstPointF.y + lY
            val secondPointF = NewPoints[if (i + 1 < NewPoints.size) i + 1 else i]
            lX = (secondPointF.x - firstPointF.x) / 2 * smoothness
            lY = (secondPointF.y - firstPointF.y) / 2 * smoothness
            val x2 = p.x - lX
            var y2 = p.y - lY
            if (y1 == p.y) {
                y2 = y1
            }
            baseLinePath!!.cubicTo(x1, y1, x2, y2, p.x, p.y)
        }
        canvas.drawPath(baseLinePath!!, mPaint!!)
        for (i in NewPoints.indices) {
            val x = NewPoints[i].x
            val y = NewPoints[i].y
            val time = timeList!![i]
            val mThumbText = time.toString()
            val rect = Rect()
            linePaint!!.getTextBounds(mThumbText, 0, mThumbText.length, rect)
            canvas.drawText(mThumbText, x - rect.width() / 2, y - dp2px(6f), linePaint!!) //画最上面字体
            canvas.drawCircle(x, y, dp2px(3f).toFloat(), circlePaint!!)
            canvas.drawCircle(x, y, dp2px(2f).toFloat(), circlePaint2!!)
            val rectf = Rect()
            textPaint!!.getTextBounds(dataList!![i], 0, dataList!![i].length, rectf)
            canvas.drawText(
                dataList!![i], x - rectf.width() / 2, sumHeight - dp2px(2f),
                textPaint!!
            ) //画最下方的字体
        }
        drawArea(canvas, NewPoints)
        drawLine(canvas)
       drawBottomLine(canvas)
    }

    /**
     * 分割线
     *
     * @param canvas
     */
    private fun drawLine(canvas: Canvas) {
        //
        var text = "0"
        val rect = Rect()
        scorePaint!!.getTextBounds(text, 0, text.length, rect)
        //0
        canvas.drawText(text, 0f, sumHeight - buttomHeiht - rect.height() / 2, textPaint!!)
        //20
        text = "20"
        val y3 = sumHeight - oneHeight * 20
        canvas.drawText(text, 0f, y3 - buttomHeiht - rect.height() / 2 - dp2px(4f), textPaint!!)
        canvas.drawLine(0f, y3 - buttomHeiht, sumWidth, y3 - buttomHeiht, scorePaint!!)
//40
        text = "40"
        val y4 = sumHeight - oneHeight * 40
        canvas.drawText(text, 0f, y4 - buttomHeiht - rect.height() / 2 - dp2px(4f), textPaint!!)
        canvas.drawLine(0f, y4 - buttomHeiht, sumWidth, y4 - buttomHeiht, scorePaint!!)

        //60
        text = "60"
        val y = sumHeight - oneHeight * 60
        canvas.drawText(text, 0f, y - buttomHeiht - rect.height() / 2 - dp2px(4f), textPaint!!)
        canvas.drawLine(0f, y - buttomHeiht, sumWidth, y - buttomHeiht, scorePaint!!)
        //80
        text = "80"
        val y5 = sumHeight - oneHeight * 80
        canvas.drawText(text, 0f, y5 - buttomHeiht - rect.height() / 2 - dp2px(4f), textPaint!!)
        canvas.drawLine(0f, y5 - buttomHeiht, sumWidth, y5 - buttomHeiht, scorePaint!!)

        //100
        text = "100"
        val y2 = sumHeight - oneHeight * 100
        canvas.drawText(text, 0f, y2 - buttomHeiht - rect.height() / 2 - dp2px(4f), textPaint!!)
        canvas.drawLine(0f, y2 - buttomHeiht, sumWidth, y2 - buttomHeiht, scorePaint!!)
    }

    /**
     * 底部标线
     *
     * @param canvas
     */
    private fun drawBottomLine(canvas: Canvas) {
        val rect = Rect()
        scorePaint!!.getTextBounds("0", 0, "0".length, rect)
        canvas.drawLine(
            0f, sumHeight - dp2px(15f) - rect.height() / 2, sumWidth,
            sumHeight - dp2px(15f) - rect.height() / 2,
            scorePaint!!
        ) //画底部灰线
    }

    /**
     * 阴影层叠
     *
     * @param canvas
     * @param Points
     */
    private fun drawArea(canvas: Canvas, Points: List<PointF>) {
        val mShader = LinearGradient(
            0f,
            0f,
            0f,
            measuredHeight.toFloat(),
            intArrayOf(
                Color.parseColor("#BAEFE6"),
                Color.parseColor("#D7F5F0"),
                Color.parseColor("#F9FEFD")
            ),
            floatArrayOf(0.5f, 0.65f, 0.85f),
            Shader.TileMode.REPEAT
        )
        baseShadow!!.shader = mShader
        if (Points.size > 0 && xyList != null && xyList!!.size > 0) {
            baseLinePath!!.lineTo(xyList!![Points.size - 1].x, sumHeight - buttomHeiht)
            baseLinePath!!.lineTo(xyList!![0].x, sumHeight - buttomHeiht)
            baseLinePath!!.close()
            canvas.drawPath(baseLinePath!!, baseShadow!!)
        }
    }

    fun dp2px(dp: Float): Int {
        val scale = this.resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    /**
     * 取出时间里面的最大的一个用来计算总长度
     *
     * @param timeList
     * @return
     */
    fun getMaxTime(timeList: ArrayList<Int>?): Float {

        maxTime = 0f
        for (i in 0..6) {
            if (timeList != null) {
                if (maxTime < timeList[i]) {
                    maxTime = timeList[i].toFloat()
                }
            }
        }
        if (maxTime <= 10) {
            maxTime = 10f
        }
        maxTime *= 1.2.toFloat()
        return maxTime
    }

    fun dp2px(context: Context, dp: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    fun dp2px(resources: Resources, dp: Float): Float {
        val scale = resources.displayMetrics.density
        return dp * scale + 0.5f
    }

    fun sp2px(resources: Resources, sp: Float): Float {
        val scale = resources.displayMetrics.scaledDensity
        return sp * scale
    }
}


